import userMapper from "@/mapper/user.mapper";
import { md5password } from "@/utils/md5Password";
import { Result, errCodeAndMsg } from "@/utils/result";

const verifyUser = async (ctx, next) => {
    // 验证客户端传递过来的user是否可以保存到数据库中
    // 1.验证用户名和密码是否为空
    const { name, password } = ctx.request.body;
    if (!name || !password) {
        return Result.err(ctx, errCodeAndMsg.USER_ACCOUNT_IS_BLANK);
    }

    // 2.判断name是否在数据库中已经存在
    const users: any = await userMapper.findUserByName(name);
    if (users.length) {
        return Result.err(ctx, errCodeAndMsg.USER_ACCOUNT_ALREADY_EXIST);
        // return ctx.app.emit('error', NAME_IS_ALREADY_EXISTS, ctx)
    }

    // 3.执行下一个中间件
    await next();
};

const handlePassword = async (ctx, next) => {
    // 1.取出密码
    const { password } = ctx.request.body;

    // 2.对密码进行加密
    ctx.request.body.password = md5password(password);

    // 3.执行下一个中间件
    await next();
};

export { verifyUser, handlePassword };
